package com.hailiao.beidou.message.data.setting

import com.hailiao.beidou.message.bd.Beidou
import com.hailiao.beidou.message.bd.constant.ProtoVersion
import com.hailiao.beidou.message.bd.sp.SPMode
import com.hailiao.beidou.message.data.store.contant.CommunicationMode
import com.hailiao.beidou.message.data.store.data.SPSettingData
import com.hailiao.beidou.message.data.store.getSpSPMode
import com.hailiao.beidou.message.data.store.getSpfCommMode
import com.hailiao.beidou.message.data.store.getSpfProtoVersion
import com.hailiao.beidou.message.data.store.getSpfSPSetting
import com.hailiao.beidou.message.data.store.setSpfCommMode
import com.hailiao.beidou.message.data.store.setSpfProtoVersion
import com.hailiao.beidou.message.data.store.setSpfSPMode
import com.hailiao.beidou.message.data.store.setSpfSPSetting
import kotlinx.coroutines.flow.MutableStateFlow

/**
 * 设置
 * @Author d10ng
 * @Date 2024/3/4 16:33
 */
object SettingRepository {

    // 协议版本
    val protoFlow = MutableStateFlow(getSpfProtoVersion())

    /**
     * 设置协议版本
     * @param value ProtoVersion
     */
    fun setProto(value: ProtoVersion) {
        setSpfProtoVersion(value)
        protoFlow.value = value
    }

    // 通讯模式
    val commModeFlow = MutableStateFlow(getSpfCommMode())

    /**
     * 设置通讯模式
     * @param value CommunicationMode
     */
    fun setCommMode(value: CommunicationMode) {
        setSpfCommMode(value)
        commModeFlow.value = value
        Beidou.setHub(value.hub)
    }

    // 串口通讯模式
    val spModeFlow = MutableStateFlow(getSpSPMode())

    /**
     * 设置串口通讯模式
     * @param value SPMode
     */
    fun setSPMode(value: SPMode) {
        setSpfSPMode(value)
        spModeFlow.value = value
    }

    // 串口通讯配置
    val spSettingFlow = MutableStateFlow(getSpfSPSetting())

    /**
     * 设置串口通讯配置
     * @param value SPSettingData
     */
    fun setSPSetting(value: SPSettingData) {
        setSpfSPSetting(value)
        spSettingFlow.value = value
    }
}